/*
 * 类名称:InvoicingServiceImpl.java
 * 包名称:com.platform.service.impl
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2019-08-28 13:43:58        lipengjun     初版做成
 *
 * Copyright (c) 2019-2019 微同科技
 */
package com.platform.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.entity.GoodsSailNumCountingEntity;
import com.platform.entity.ShopCounting;
import com.platform.entity.StockEntity;
import com.platform.utils.Query;
import com.platform.utils.QueryPlus;
import com.platform.dao.InvoicingDao;
import com.platform.entity.InvoicingEntity;
import com.platform.service.InvoicingService;
import com.platform.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 平台进销记录表Service实现类
 *
 * @author sunkaichun
 * @date 2019-08-28 13:43:58
 */
@Service("invoicingService")
public class InvoicingServiceImpl extends ServiceImpl<InvoicingDao, InvoicingEntity> implements InvoicingService {

    @Autowired
    private InvoicingDao invoicingDao;
    @Override
    public List<InvoicingEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.id");
        params.put("asc", false);
        Page<InvoicingEntity> page = new QueryPlus<InvoicingEntity>(params).getPage();
        return page.setRecords(baseMapper.selectInvoicingPage(page, params));
    }

    @Override
    public Page shopCounting (Map<String, Object> params) {
        Page<ShopCounting> page = new QueryPlus<ShopCounting>(params).getPage();
        List<ShopCounting> shopCountings = invoicingDao.shopCounting(params);
        return page.setRecords(shopCountings);
    }

    @Override
    public Page goodsCounting (Map<String, Object> params) {
        Page<GoodsSailNumCountingEntity> page = new QueryPlus<GoodsSailNumCountingEntity>(params).getPage();
        return page.setRecords(baseMapper.goodsCounting(params));
    }

    @Override
    public boolean add(InvoicingEntity invoicing) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmsss");
        String orderNo = df.format(new Date());
        invoicing.setOrderNo(orderNo);
        invoicing.setCreateTime(new Date());
        Long userId = ShiroUtils.getUserId();
        invoicing.setCreateUserId(userId);
        return this.save(invoicing);
    }

    @Override
    public boolean update(InvoicingEntity invoicing) {
        invoicing.setUpdateTime(new Date());
        Long userId = ShiroUtils.getUserId();
        invoicing.setUpdateUserId(userId);
        return this.updateById(invoicing);
    }

    @Override
    public boolean delete(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(Integer[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public List<InvoicingEntity> queryList(Query query) {
        return baseMapper.queryList(query);
    }

    @Override
    public int queryTotal(Query query) {
        return baseMapper.queryTotal(query);
    }

    @Override
    public List<StockEntity> queryStockList(Query query) {
        return baseMapper.queryStockList(query);
    }

    @Override
    public int queryStockListTotal(Query query) {
        return baseMapper.queryStockListTotal(query);
    }
}
